//
//  WSMusicDetailsViewController.m
//  RunningMan
//
//  Created by 王帅 on 16/5/23.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSMusicDetailsViewController.h"
#import "WSMusicDetailsCell.h"
#import "WSMusicModel.h"
#import "WSAFNetWorkingTool.h"
#import "WSMyTool.h"
#import "WSMusicDetailsView.h"
#import "WSPlayerViewController.h"
@interface WSMusicDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) WSMusicDetailsView *musicDetailsView;

@end

@implementation WSMusicDetailsViewController

-(NSMutableArray *)listArray {
    if (_listArray == nil) {
        
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

-(NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"歌曲列表";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self createTableView];
    
    self.musicDetailsView = [[WSMusicDetailsView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 350)];
    self.tableView.tableHeaderView = self.musicDetailsView;
    
    [self getData];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[WSMusicDetailsCell class] forCellReuseIdentifier:NSStringFromClass([WSMusicDetailsCell class])];
    self.tableView.rowHeight = 100;
}

- (void)getData {
    
    NSString *url = @"http://wawa.fm:9090/wawa/api.php/user/playlistinfo/";
    NSDictionary *header = @{@"Content-Length" : @"28", @"Content-Type" : @"application/x-www-form-urlencoded", @"Host" : @"wawa.fm:9090"};
    NSDictionary *body = @{@"platform" : @"wwandroid", @"uid" :  [NSString stringWithFormat:@"%@", self.ID]};
    [WSAFNetWorkingTool numberPostWithURL:url withBody:body withBodyType:BodyTypeJSONString withHttpHeader:header withResponseType:ResponseTypeJSON withSuccess:^(id result) {
    
        
        [self.musicDetailsView.imageViewPhoto sd_setImageWithURL:[NSURL URLWithString:result[@"playlist_small_image"]] placeholderImage:nil];
        [self.musicDetailsView.imageViewUserName sd_setImageWithURL:[NSURL URLWithString:result[@"pimg"]] placeholderImage:nil];
        self.musicDetailsView.labelUserName.text = result[@"username"];
        self.musicDetailsView.labelPlayList.text = result[@"playlist_name"];
        
        NSArray *listen = result[@"listen"];
        for (NSDictionary *dic in listen) {
            NSString *url = dic[@"pimg"];
            [self.imageArray addObject:url];
            
        }
        
        
        NSArray *tracks = result[@"tracks"];
        for (NSDictionary *dic in tracks) {
            WSMusicModel *model = [[WSMusicModel alloc] init];
            model.songer = dic[@"songer"];
            model.songname = dic[@"songname"];
            model.thumbnail_url = dic[@"thumbnail_url"];
            model.filename = dic[@"filename"];
            [self.listArray addObject:model];
        }
        [self.tableView reloadData];
        
    } withFail:^(NSError *error) {
        
        
    }];
 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSMusicDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSMusicDetailsCell class])];
    WSMusicModel *model = self.listArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WSPlayerViewController *player = [WSPlayerViewController sharePlayMusic];
    player.hidesBottomBarWhenPushed = YES;
    player.arrayMusic = [self.listArray mutableCopy];
    player.index = indexPath.row;
    [self.navigationController pushViewController:player animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*imageViewPhoto
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
